package org.wanho.servlet;

import org.wanho.ums.User;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Author：汤小洋
 * Date：2023-04-24 15:41
 * Description：<描述>
 */
@WebServlet("/data")
public class DataServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 向request作用域中存储数据
         */
        req.setAttribute("age",20);
        req.setAttribute("sex","女");

        /**
         * 向session作用域中存储数据
         */
        req.getSession(true).setAttribute("sex","男");


        /**
         * 向application作用域中存储数据
         */
        ServletContext application = req.getServletContext(); // 两种方式都可以获取application
        // ServletContext application = req.getSession().getServletContext();
        application.setAttribute("address","南京");

        req.getSession().setAttribute("user", new User(101,"aaa","123",18,"吃饭","aaa","男"));

        List<User> users = new ArrayList<>();
        users.add(new User(101,"aaa","123",18,"吃饭","aaa","男"));
        users.add(new User(102,"bbb","123",18,"吃饭","aaa","男"));
        users.add(new User(103,"ccc","123",18,"吃饭","aaa","男"));
        req.setAttribute("users",users);

        req.setAttribute("birthday", new Date());

        System.out.println("执行DataServlet。。。。。。。。。。。。");

        req.getRequestDispatcher("/test08.jsp").forward(req,resp);

    }
}
